<?php

/**
 * passwords actions.
 *
 * @package    pwdb
 * @subpackage passwords
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class passwordsActions extends sfActions
{
  /**
   * Executes index action
   *
   */
  public function executeIndex()
  {
  }
  
  /**
   * Passwords
   *
   */
  public function executeList()
  {
    $this->groups    = $this->getUser()->getGuardUser()->getPasswordGroups();
    $this->passwords = $this->getUser()->getGuardUser()->getPasswords()->toArray();
    
  }
  
  public function executeAdd()
  {
    $this->password = $this->getUser()->getGuardUser()->getPasswords()->get(null);
    $this->password->setTitle("Foo");
    $this->password->setGroup($this->getUser()->getGuardUser()->getPasswordGroups()->getFirst());
    $this->password->save();
    $this->redirect('passwords/edit');
  }
  
  public function executeEdit()
  {
    $this->id       = $this->getRequestParameter("id");
    $this->group = $this->getRequestParameter("group");
    $this->password = $this->getUser()->getGuardUser()->getPasswordGroups()->get($this->getRequestParameter("group"))->getPasswords()->get($this->getRequestParameter("id"));
  }
  
  public function executeUpdate()
  {
    $password = $this->getUser()->getGuardUser()->getPasswordGroups()->get($this->getRequestParameter("group"))->getPasswords()->get($this->getRequestParameter("id"));
    $password->merge($this->getRequestParameter("password"));
    $password->save(); 
    return sfView::NONE;
  }
  
  public function executeShowPassword()
  {
    $this->password = $this->getUser()->getGuardUser()->getPasswordGroups()->get($this->getRequestParameter("group"))->getPasswords()->get($this->getRequestParameter("id"));
  }
  
  /**
   * Groups
   *
   */
  public function executeAddGroup()
  {
    $this->group =  $this->getUser()->getGuardUser()->getPasswordGroups()->get(null);
    $this->group->setTitle("FooGroup");
    $this->group->save();
    $this->redirect('passwords/editGroup/?id');
  }
  
  public function executeEditGroup()
  {
    $this->id = $this->getRequestParameter("id");
    $this->group = $this->getUser()->getGuardUser()->getPasswordGroups()->get($this->getRequestParameter("id"));
  }
  
  public function executeUpdateGroup()
  {
    $group = $this->getUser()->getGuardUser()->getPasswordGroups()->get($this->getRequestParameter("id"));
    $group->merge($this->getRequestParameter("group"));
    $group->save(); 
    return sfView::NONE;
  }

  public function executeMovePassword()
  {
    $password_split = explode("_", $this->getRequestParameter("id"));
    $password = $this->getUser()->getGuardUser()->getPasswordGroups()->get($password_split[1])->getPasswords()->get($password_split[2]);
    $password->setGroup($this->getUser()->getGuardUser()->getPasswordGroups()->get($this->getRequestParameter("group")));
    $password->save();
    return sfView::NONE;
  }
}
